package org.jeecg.modules.wlhy.hycarrier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.wlhy.hycarrier.entity.HyZhenCarrierOrderDriver;

import org.jeecg.modules.wlhy.hycarrier.mapper.HyZhenCarrierOrderDriverMapper;
import org.jeecg.modules.wlhy.hycarrier.service.IHyZhenCarrierOrderDriverService;

import org.jeecg.modules.wlhy.hyvehicle.entity.HyDriverVehicle;
import org.jeecg.modules.wlhy.hyzhencarrier.entity.HyZhenCarrier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 承运商
 * @Author: duanlikao
 * @Date: 2020-12-01
 * @Version: V1.0
 */
@Service
public class HyZhenCarrierOrderDriverServiceImpl extends ServiceImpl<HyZhenCarrierOrderDriverMapper, HyZhenCarrierOrderDriver> implements IHyZhenCarrierOrderDriverService {


    @Override
    public boolean binding(String driverId, String zhenCarrier) {

        int count = this.count(new QueryWrapper<HyZhenCarrierOrderDriver>()
                .eq("driver_id", driverId)
                .eq("zhen_carrier_id", zhenCarrier)
        );
        if(count==0){
            HyZhenCarrierOrderDriver hyZhenCarrierOrderDriver = new HyZhenCarrierOrderDriver();
            hyZhenCarrierOrderDriver.setDriverId(driverId);
            hyZhenCarrierOrderDriver.setZhenCarrierId(zhenCarrier);
            return this.save(hyZhenCarrierOrderDriver);
        }else{
            return false;
        }

    }

    @Override
    public boolean bindingOrder(String orderId, String zhenCarrier) {
        int count = this.count(new QueryWrapper<HyZhenCarrierOrderDriver>()
                .eq("order_id", orderId)
                .eq("zhen_carrier_id", zhenCarrier)
        );
        if(count==0){
            HyZhenCarrierOrderDriver hyZhenCarrierOrderDriver = new HyZhenCarrierOrderDriver();
            hyZhenCarrierOrderDriver.setOrderId(orderId);
            hyZhenCarrierOrderDriver.setZhenCarrierId(zhenCarrier);
            return this.save(hyZhenCarrierOrderDriver);
        }else{
            return false;
        }
    }

    @Override
    public List<String> getDriverIdListByCarrierId(String carrierId) {
        if(StringUtils.isEmpty(carrierId)){
            return new ArrayList<>();
        }
        QueryWrapper <HyZhenCarrierOrderDriver> queryWrapper = new QueryWrapper();
        queryWrapper.eq("zhen_carrier_id", carrierId);
//        queryWrapper.eq("iz_used", 1);
        List<HyZhenCarrierOrderDriver> listDriverVehicle = this.list(queryWrapper);

        return  CollectionUtils.isNotEmpty(listDriverVehicle)  ?
                listDriverVehicle.stream().map(HyZhenCarrierOrderDriver::getDriverId).collect(Collectors.toList())
                : new ArrayList <>();
    }

    @Override
    public List<String> getOrderIdListByCarrierId(String carrierId) {
        if(StringUtils.isEmpty(carrierId)){
            return new ArrayList<>();
        }
        QueryWrapper <HyZhenCarrierOrderDriver> queryWrapper = new QueryWrapper();
        queryWrapper.eq("zhen_carrier_id", carrierId);
//        queryWrapper.eq("iz_used", 1);
        List<HyZhenCarrierOrderDriver> listDriverVehicle = this.list(queryWrapper);

        return  CollectionUtils.isNotEmpty(listDriverVehicle)  ?
                listDriverVehicle.stream().map(HyZhenCarrierOrderDriver::getOrderId).collect(Collectors.toList())
                : new ArrayList <>();
    }
}
